package _20250227;

import java.util.Arrays;
import java.util.OptionalInt;

public class FoundMaxMin {
    /**
     *4.从一个包含整数的数组int[] numbers出发，
     * 利用 Stream 流找出数组中的最大值和最小值，并将这两个值组成一个新的数组返回
     */
    public static void main(String[] args) {
        int[] numbers1 = {12, 5, 8, 19, 3, 27, 11};
        int[] numbers2 = {0, 0, 0, 0, 0};
        int[] numbers3 = {-5, -10, -3, -8, -1};
        int[] numbers4 = {Integer.MAX_VALUE, 100, 500, 200};
        int[] numbers5 = {Integer.MIN_VALUE, -100, -200, -50};
        int[] numbers6 = {100};
        int[] numbers7 = {};
        int[] numbers8 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] numbers9 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
        int[] numbers10 = {5, 15, 5, 25, 5, 35, 5};


        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers1)));
        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers2)));
        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers3)));
        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers4)));
        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers5)));
        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers6)));
        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers7)));
        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers8)));
        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers9)));
        System.out.println(Arrays.toString(foundMaxAndMinNumber(numbers10)));
    }

    /**
     * 返回最大值和最小值
     * @param numbers 数据
     * @return 返回最大值和最小是
     */

    private static int[] foundMaxAndMinNumber(int[] numbers) {
        if (numbers == null || numbers.length == 0) {
            throw new IllegalArgumentException("数组不能为空");
        }
      OptionalInt max= Arrays.stream(numbers).max();
      OptionalInt min= Arrays.stream(numbers).min();
      return new int[]{max.getAsInt(), min.getAsInt()};
    }

}
